Understanding and optimizing fish counting techniques based on electrical impedance measurements

Sustainable aquatic resources management requires reliable methods for fish detection in various environmental conditions. Herein, we study fundamental mechanisms underlying the application of electrical impedance measurements in this regard. We present results of experimental studies conducted in laboratory conditions using a low-cost impedance measurement circuit, as well as the corresponding numerical models. We also present evaluation results of a newly developed, real-time detection algorithm based on adaptive thresholding. The numerical model was validated by extracting fish tracks in 3D space from the experimental datasets, and then comparing the calculated versus measured impedance values as functions of fish coordinates in time. Numerical predictions closely resemble the experimental data. The detection sensitivity and specificity values determined for various settings exceeded 90%. Electrode width to spacing ratio is demonstrated to be a crucial parameter influencing the system sensitivity distribution. The introduced approach can constitute a framework for designing electrical impedance-based fish counting systems.

Lukasz J. Nowak, Martin Lankheet Notation p x r -real-world x-coordinate, expressed in [m] p y r -real-world y-coordinate, expressed in [m] p z r -real-world z-coordinate, expressed in [m] p x p -pixel-wise x-coordinate.Determined from the image, expressed in pixels p y p -pixel-wise y-coordinate.Determined from the image, expressed in pixels p z p -pixel-wise z-coordinate.Determined from the image, expressed in pixels

2,5-D transformation algorithm
The fish localization algorithm utilizes two cameras looking at the tank from two perpendicular angles: from top and side of the tank.The cameras are synchronized, so that corresponding frames in the corresponding recordings are assumed to present snapshots of the corresponding moments in time.Here, it is assumed that Camera 1 is recording from the top (its sensor is arranged in XY plane), and Camera 2 is capturing the side view of the tank (its sensor is arranged in YZ plane).We used two GoPro Hero10 cameras operating in Linear mode.It was assumed that non-linear distortions remaining after the calibration procedure are relatively small compared to fish dimensions and accuracy of coordinates detection based on contour identification.
In order to determine fish location inside the tank reference points with known real-world coordinates are needed (e.g., relative to one of the tank corners).Here, we use reference points arranged in two parallel planes (parallel to themselves and to camera sensor planes).There are four points per plane, forming vertices of rectangles on the tank walls.In the experimental setup we used a small magnetic balls painted in bright yellow, which were later on marked on camera frames to read their pixel coordinates on images from both cameras.We also knew their real-world coordinates inside the tank.This is illustrated in Figure 1.Points corresponding to two reference planes for each camera are marked in blue and red.
The localization algorithm consists of the following steps:  Determining fish localization in the video frames.
Video frames are processed one by one, utilizing the procedure described herein to each single image pair.First, the frames are color-filtered by applying HSV (hue, saturation, value) thresholds to each image.The threshold values were determined manually on randomly selected frames, so that only the fish was visible in the filtered images.They are specific for given optical properties of fish, background, and lightning conditions.Any residual artifacts were significantly smaller than the fish size.The procedure is illustrated in Figure 2. The color filtering procedure does not remove fish reflections from the glass tank walls.However, they can be easily identified and removed afterwards, by selecting only the object closest to the middle of the tank.
Fish contours are extracted from the processed images using OpenCV library in Python and cv2.findContours() function.Fish coordinates (pixel-wise) are determined as coordinates of the center of gravity of the contour.

Transformation between pixel and real-world coordinates
It is assumed that the cameras are perfectly perpendicular and look straight into the parallel reference planes.It is also assumed that the transformation between pixel and real-world coordinate system includes translation, rotation and scaling.The transformation matrices and equation can be expressed as follows: where: is the real-world coordinate vector for Camera 1 and a given reference plane.
The corresponding vector for Camera 2 will only differ in superscripts, having yz instead of xy coordinates.The p z r coordinates of the reference points for Camera 1 and p x r coordinates of the reference points for Camera 2 are known (from the set locations of the markers).
is the pixel coordinate vector for camera no. 1, and: is the transformation matrix, being multiplication product of translation T , rotation R, and scaling S matrices, expressed with the following equations: where t x and t y are translation coefficients, θ is rotation angle between the coordinate systems, and s is the scaling factor.Thus: where A = s cosθ, and B = s sinθ.Thus, we can rewrite the equation (3) as: In order to determine real-world fish coordinates we first calculate A, B, t x , and t y , based on the known real-world and pixel coordinates of the reference points in the tank (Figure 1).The calculations of the sought T RS matrices' coefficients need to be performed independently for Camera 1 and Camera 2, and for the corresponding reference planes.Generalizing the problem into n reference points for each reference plane and assuming n ≥ 2, we use linear least squares (LLS) method and the following matrix equations (xy coordinates refer to Camera 1.For Camera 2 they are replaced with the corresponding yz coordinates): where: is the pixel-coordinate matrix of reference points, with dimensions 2n × 4 (n is the number of reference points in a given plane).p x pm is the x pixel-coordinate of the reference point no.m.
−→ X R is the vector of the corresponding real-world coordinates of the n reference points: and − → M is the vector of the sought transformation coefficients: Under such conditions we can approximate − → M with its best match −→ M * based on the LLS principle:

Point localization in 3D space
The pixel coordinates of the fish read from the corresponding frames from Camera 1 and Camera 2 are first converted into four points corresponding to the coordinates in all four reference planes (i.e., two for each camera view).This is done using equation (1) with matrix coefficients calculated using equation ( 14) and the reference points' coordinates.The result are four points defined with real-world coordinates corresponding to four reference planes: where subscripts denote point numbers (corresponding to reference plane numbers) and superscripts denote camera indices.A pair of points for each camera defines a line, which can be defined with the following vector equations: ), ( 16) where l 1 and l 2 are (yet unknown) coefficients.The sought fish coordinates in 3D space are defined by the intersection point of lines L 1 and L 2 .However, due to the inevitable errors in reading point coordinates, it is unlikely that the lines would actually intersect.We thus find the sought fish coordinates as a middle point of a vector − − → CD, defining the shortest distance between L 1 and L 2 .Following conditions apply: The first two conditions imply: and The remaining two conditions imply This can be expressed as: 21) We solve the above equations for l 1 and l 2 .To do this, we rewrite them in the following form, using relations ( 17) and ( 18): where: (23) and: This yields: and: Eventually, we substitute ( 25) and ( 26) into ( 17) and ( 18) to find the desired coordinates of the approximated location of the triangulated point of interest (fish coordinates) in 3D space:

Figure 1 :
Figure 1: Reference points inside the tank with their pixel coordinates on the images.Numbers in brackets are the points' indices.The points form two sets of vertices of rectangles, defining two reference planes (marked in blue and red) for each camera view.Point 1 is the origin with real-world coordinates (p x r , p y r , p z r ) = (0,0,0).

Figure 2 :
Figure 2: A captured video frame from Camera 1 (left), and the same frame after applying HSV color filtering (right).Only the fish and its reflection from the wall are visible.